/* @(#)FiscalYear.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fmm.node.impl.governable;

import java.util.Collection;

import org.json.JSONObject;

import com.flywheelms.library.deckangl.glyph.DecKanGlDictionary;
import com.flywheelms.library.deckangl.glyph.NounWorkStatus;
import com.flywheelms.library.fmm.FmmDatabaseMediator;
import com.flywheelms.library.fmm.node.FmmNodeInfo;
import com.flywheelms.library.fmm.node.NodeId;
import com.flywheelms.library.fmm.node.impl.completable.FmmCompletableNodeImpl;
import com.flywheelms.library.fmm.node.impl.enumerator.CompletableWorkState;
import com.flywheelms.library.fmm.node.impl.enumerator.FmmNodeDictionary;
import com.flywheelms.library.fmm.perspective.FmmPerspective;
import com.flywheelms.library.fmm.transaction.FmmNodeGlyphType;

public class FiscalYear extends FmmCompletableNodeImpl implements
		Comparable<FiscalYear> {
	
	private static final long serialVersionUID = -2661300227094514671L;
	private String organizationNodeId;
	private FmsOrganization organization;
	private String year = "0000";
	private boolean autoCompletable;
	
	public FiscalYear(NodeId aNodeId) {
		super(aNodeId);
	}
	
	public FiscalYear(String aExistingNodeIdString) {
		super(NodeId.rehydrate(
				FiscalYear.class,
				aExistingNodeIdString ));
	}

	public Object getOrganizationNodeId() {
		return this.organizationNodeId;
	}

	public void setOrganizationNodeId(String anOrganizationNodeId) {
		this.organizationNodeId = anOrganizationNodeId;
	}
	
	public FmsOrganization getOrganization() {
		if(this.organization == null && this.organizationNodeId != null) {
			this.organization = FmmDatabaseMediator.getActiveMediator().getOrganization(this.organizationNodeId);
		}
		return this.organization;
	}
	
	public void setOrganization(FmsOrganization anOrganization) {
		this.organization = anOrganization;
	}
	
	public String getYear() {
		return this.year;
	}
	
	public int getYearAsInt() {
		return Integer.decode(this.year);
	}

	public void setYear(String aYear) {
		this.year = aYear;
	}

	public boolean getAutoCompletable() {
		return this.autoCompletable;
	}

	public void setAutoCompletable(int bAutoCompletable) {
		this.autoCompletable = bAutoCompletable < 1 ? false : true;
	}
	
	@Override
	public int compareTo(FiscalYear anOtherFiscalYear) {
		return (getYearAsInt() < anOtherFiscalYear.getYearAsInt() ? -1 :
			(getYearAsInt() == anOtherFiscalYear.getYearAsInt() ? 0 : 1));
	}

	@Override
	protected void initializeNodeCompletionSummaryMap() {
		super.initializeNodeCompletionSummaryMap();
		NodeCompletionSummary theNodeCompletionSummary = new NodeCompletionSummary();
		theNodeCompletionSummary.setSummaryDrawableResourceId(
				FmmNodeDictionary.STRATEGIC_MILESTONE.getUndecoratedGlyphResourceId(FmmNodeGlyphType.GREEN) );
		updateNodeCompletionSummary(FmmPerspective.STRATEGIC_PLANNING, theNodeCompletionSummary);
		this.nodeCompletionSummaryMap.put(FmmPerspective.STRATEGIC_PLANNING, theNodeCompletionSummary);
	}

	@Override
	public void updateNodeCompletionSummary(FmmPerspective anFmmPerspective, NodeCompletionSummary aNodeSummary) {
		switch(anFmmPerspective) {
		case STRATEGIC_PLANNING:
			Collection<StrategicMilestone> theStrategicMilestoneCollection = getStrategicMilestoneCollection();
			if(theStrategicMilestoneCollection.size() > 0) {
				aNodeSummary.setShowNodeSummary(true);
				aNodeSummary.setSummaryPrefix("( " + countGreenStrategicMilestones() + " ");
				aNodeSummary.setSummarySuffix(" of " + theStrategicMilestoneCollection.size() + " )");
			} else {
				aNodeSummary.setShowNodeSummary(false);
			}
			break;
		default:
			break;
		}
	}

	private Collection<StrategicMilestone> getStrategicMilestoneCollection() {
		return FmmDatabaseMediator.getActiveMediator().getStrategicMilestoneCollection(this);
	}

	private int countGreenStrategicMilestones() {
		int theGreenCount = 0;
		for(StrategicMilestone theStrategicMilestone : getStrategicMilestoneCollection()) {
			if(theStrategicMilestone.isGreen()) {
				++theGreenCount;
			}
		}
		return theGreenCount;
	}
	
}
